package product;

import java.util.ArrayList;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class work {

	/**
	 * 1、统计出搜索过“%百度影音%”的用户(UID)还搜索过哪些关键字。
	思路：
	两个Map:
	第一个map:搜索过“%百度影音%”的用户(UID)
	第二个map:全部的uid,keyword
	一个reduce:
	sogou数据格式：
	timestamp\tuid\tkeyword\trank\torder\turl，
	访问时间\t用户ID\t搜索关键词\t结果排序\t点击次数\t用户最后点击的URL
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		if (args.length!=3) {
			System.err.println("path err");
			System.exit(-1);
		}
		Job job=new Job(new Configuration(),"no");
		job.setJarByClass(work.class);
		
		MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class,baimap.class);
		MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, allmap.class);
		
		FileOutputFormat.setOutputPath(job, new Path(args[2]));
		job.setReducerClass(myreduce.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.waitForCompletion(true);

	}
	public static class baimap extends Mapper<LongWritable, Text, Text, Text>{
		public static final String LABEL="w_";
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String[] lines = value.toString().split(",");
			String keyword = lines[2].trim();
			if (lines.length==6 && keyword.contains("百度影音")) {
				String uid = lines[1].trim();
				context.write(new Text(uid), new Text(LABEL+keyword));
			}
		};
	}
	public static class allmap extends Mapper<LongWritable, Text, Text, Text>{
		public static final String RIGHT="s_";
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String[] lines = value.toString().split(",");
			if (lines.length==6) {
				context.write(new Text(lines[1].trim()), new Text(RIGHT+lines[2].trim()));
			}
		};
	}
	public static class myreduce extends Reducer<Text, Text, Text, Text>{
		protected void reduce(Text k2, java.lang.Iterable<Text> values, org.apache.hadoop.mapreduce.Reducer<Text,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String keywords=null;
			ArrayList<String> list=new ArrayList<String>();
			for (Text str : values) {
				String info=str.toString();
				if (info.startsWith(baimap.LABEL)) {
					list.add(info.substring(2));
				}else if(info.startsWith(allmap.RIGHT)){
					keywords=info.substring(2);
				}
			}
			if (keywords==null && list.size()>0) {
				for (String kw : list) {
					context.write(k2,new Text(kw));
				}
			}
		};
	}

}
